<template>
  <el-dialog
    :visible.sync="visible"
    :append-to-body="false"
    :before-close="handleCancel"
    class="dialog-size-auto small-common-dialog-size"
    width="450px">
    <div class="main">
      <Title title="设置通知人" font-size="16px" style="margin-bottom:16px;"/>
      <el-form
        ref="projectForm"
        :model="form"
        label-position="right"
        label-width="75px"
        class="lineHeight32">
        <el-form-item label="通知人" prop="followers" class="followers">
          <selectWithUserLoad
            ref="proposerRef"
            v-model="form.followers"
            :default-list="defaultList"
            :multiple="true"
            :value-type="'object'"
            :show-check-user="true"
            key-name="id"
            class="width240"
            label-name="userName"
            filterable
            clearable
          />
        </el-form-item>
      </el-form>
      <div class="btn-container">
        <el-button size="small" type="primary" @click="handleConfirm">确定</el-button>
        <el-button type="info" class="cancel-btn-style" size="small" @click="handleCancel">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import Title from '@/components/Title';
import SelectWithUserLoad from '@/components/Common/selectWithUserLoad';
import { editReportDetail } from '@/api/publish';

export default {
  components: { Title, SelectWithUserLoad },
  data() {
    return {
      visible: false,
      id: '',
      defaultList: [],
      form: {
        followers: ''
      }
    };
  },
  methods: {
    openDialog(obj) {
      this.id = obj.id;
      this.defaultList = [];
      obj.followers.forEach(j => {
        this.defaultList.push({ id: j.id, userName: j.name });
      });
      this.form.followers = this.defaultList;
      this.visible = true;
    },
    handleConfirm() {
      // 校验必填
      this.$refs['projectForm'].validate((valid) => {
        if (valid) {
          const followers = [];
          this.form.followers.forEach(j => {
            followers.push({ name: j.userName, id: j.id });
          });
          editReportDetail(this.id, { followers }).then(res => {
            this.$emit('updateData');
            this.handleCancel();
          }).catch(err => {
            console.log(err);
          });
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.$refs['projectForm'].resetFields();
      this.visible = false;
    }
  }
};
</script>

<style lang="scss" scoped>
.width240 {
  width: 240px;
}
.lineHeight32 {
  margin-bottom: 24px;
}
.btn-container {
  display: flex;
  justify-content: center;
}
</style>
